package com.coolw.design.pattern.behavior.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * OrderList
 *
 * @author coolw
 * @date 2025/4/3 13:36
 */
public class OrderList {

    private final List<Order> orders;

    public OrderList() {
        orders = new ArrayList<>();
    }

    /**
     * 添加订单
     */
    public void add(Order order) {
        orders.add(order);
    }

    /**
     * 迭代器
     */
    public Iterator<Order> iterator() {
        return new OrderIterator();
    }

    private class OrderIterator implements Iterator<Order> {
        private int index;

        @Override
        public boolean hasNext() {
            return index < orders.size();
        }

        @Override
        public Order next() {
            if (hasNext()) {
                Order order = orders.get(index);
                index++;
                return order;
            } else {
                throw new IndexOutOfBoundsException();
            }
        }
    }
}
